const router = require('koa-router')()

const apiBase = require('./apiBase');
const Boat = require('./../model/boat')
router.prefix(apiBase + '/member')

let sees = require('./../utils/sess')

let memberFn = require('./../controller/member/index');

let inspectBoatId = async (ctx, next)=> {
    let body = ctx.request.body;
    console.log(body)
    if (!body.boatId) {
        ctx.body = new resModel.Error('队伍id必填');
    } else {
        let cb = await Boat.findById(body.boatId)
        if (cb) {
            await next()
        } else {
            ctx.body = new resModel.Due('该队伍已不存在，不能添加到该队伍中');
        }
    }
}


router.post('/create', inspectBoatId, memberFn.create)

router.post('/update', sees.autoToken, inspectBoatId, memberFn.update)

router.post('/remove', sees.autoToken, inspectBoatId, memberFn.remove)

router.post('/list', sees.autoToken, inspectBoatId, memberFn.list)

module.exports = router;